﻿using SlamDunk.CMS.Model.Base;
using SlamDunk.CMS.Model.FormatModel;
using SlamDunk.CMS.Model.ViewModel;

namespace SlamDunk.CMS.Model.DataModel
{
    /// <summary>
    /// 栏目
    /// </summary>
    public class Channel : BaseEntity
    {
        #region # 属性

        #region 上级栏目Id —— int PId
        /// <summary>
        /// 上级栏目Id
        /// </summary>
        public int PId { get; set; }
        #endregion

        #region 栏目名称 —— string ChannelName
        /// <summary>
        /// 栏目名称
        /// </summary>
        public string ChannelName { get; set; }
        #endregion

        #region 缩写 —— string Abbr
        /// <summary>
        /// 缩写
        /// </summary>
        public string Abbr { get; set; }
        #endregion

        #region 描述 —— string Description
        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; set; }
        #endregion

        #region 保存路径 —— string SaveDirectory
        /// <summary>
        /// 保存路径
        /// </summary>
        public string SaveDirectory { get; set; }
        #endregion

        #region 聚合页模板路径 —— string IndexTemplate
        /// <summary>
        /// 聚合页模板路径
        /// </summary>
        public string IndexTemplate { get; set; }
        #endregion

        #region 列表页模板路径 —— string ListTemplate
        /// <summary>
        /// 列表页模板路径
        /// </summary>
        public string ListTemplate { get; set; }
        #endregion

        #region 内容页模板路径 —— string DetailTemplate
        /// <summary>
        /// 内容页模板路径
        /// </summary>
        public string DetailTemplate { get; set; }
        #endregion

        #region 页容量 —— int PageSize
        /// <summary>
        /// 页容量
        /// </summary>
        public int PageSize { get; set; }
        #endregion

        #region 排序 —— int? Sort
        /// <summary>
        /// 排序
        /// </summary>
        public int? Sort { get; set; }
        #endregion

        #region 是否是链接 —— bool IsLink
        /// <summary>
        /// 是否是链接
        /// </summary>
        public bool IsLink { get; set; }
        #endregion

        #region 是否是导航 —— bool IsNavigator
        /// <summary>
        /// 是否是导航
        /// </summary>
        public bool IsNavigator { get; set; }
        #endregion

        #endregion

        #region # 方法

        #region 转换为视图模型 —— ChannelView ToViewModel()
        /// <summary>
        /// 转换为视图模型
        /// </summary>
        /// <returns>视图模型</returns>
        public ChannelView ToViewModel()
        {
            return new ChannelView()
            {
                Id = this.Id,
                PId = this.PId,
                ChannelName = this.ChannelName,
                Abbr = this.Abbr,
                Description = this.Description,
                IndexTemplate = this.IndexTemplate,
                ListTemplate = this.ListTemplate,
                DetailTemplate = this.DetailTemplate,
                PageSize = this.PageSize,
                Sort = this.Sort,
                IsLink = this.IsLink,
                IsNavigator = this.IsNavigator,
                Url = this.GetUrl()
            };
        }
        #endregion

        #region 转换为树形节点 —— TreeModel ToTreeNode()
        /// <summary>
        /// 转换为树形节点
        /// </summary>
        /// <returns>树形节点</returns>
        public TreeModel ToTreeNode()
        {
            //返回TreeModel
            return new TreeModel()
            {
                id = this.Id,
                text = this.ChannelName,
                state = "open",
                attributes = new { href = string.Format("/Admin/Article/Index/{0}", this.Id), isLink = this.IsLink == true ? 1 : 0 },
                @checked = false
            };
        }
        #endregion

        #region 获取栏目Url地址 —— string GetUrl()
        /// <summary>
        /// 获取栏目Url地址
        /// </summary>
        /// <returns>栏目Url地址</returns>
        public string GetUrl()
        {
            if (this.IsLink)
            {
                string indexPage = string.Format("{0}/Index.html", this.SaveDirectory);
                string listPage = string.Format("{0}/List-1.html", this.SaveDirectory);
                if (!string.IsNullOrEmpty(this.IndexTemplate))
                {
                    return indexPage;
                }
                if (!string.IsNullOrEmpty(this.ListTemplate))
                {
                    return listPage;
                }
            }
            return string.Empty;
        }
        #endregion

        #endregion
    }
}
